<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="维修单号" prop="repairNumber">
        <el-input
          v-model="queryParams.repairNumber"
          placeholder="请输入维修单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障名称" prop="faultName">
        <el-input
          v-model="queryParams.faultName"
          placeholder="请输入故障名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有保修单" prop="repairBill">
        <el-input
          v-model="queryParams.repairBill"
          placeholder="请输入是否有保修单"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障项目初始分类" prop="initCategory">
        <el-select
          v-model="queryParams.initCategory"
          placeholder="故障项目初始分类"
          clearable

        >
        <el-option
          v-for="dict in dict.type.gelly_fault_category"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        />
        </el-select>
      </el-form-item>
      <el-form-item label="目前分类" prop="curCategory">
        <el-select
          v-model="queryParams.curCategory"
          placeholder="目前分类"
          clearable
        >
          <el-option
            v-for="dict in dict.type.gelly_fault_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跟踪负责人" prop="responsible">
        <el-input
          v-model="queryParams.responsible"
          placeholder="请输入跟踪负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间节点" prop="timeNode">
        <el-date-picker clearable
          v-model="queryParams.timeNode"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择时间节点">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否复盘" prop="review">
        <el-input
          v-model="queryParams.review"
          placeholder="请输入是否复盘"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="owner">
        <el-input
          v-model="queryParams.owner"
          placeholder="请输入责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['gelly:report:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gelly:report:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['gelly:report:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gelly:report:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="追踪ID" align="center" prop="trackId" />
      <el-table-column label="维修单号" align="center" prop="repairNumber" width="110px">
        <template slot-scope="scope">
          <router-link tag="a" target="_blank" :to="{ path: '/gelly/report/pdf', query: { trackId: scope.row.trackId } }" class="link-type">
            <span>{{scope.row.repairNumber}}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="是否有保修单" align="center" prop="repairBill" />
      <el-table-column label="设备编号" align="center" prop="deviceNumber" width="180" />
      <el-table-column label="制造厂家" align="center" prop="manufacturer" />
      <el-table-column label="规格型号" align="center" prop="specifications" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="工段" align="center" prop="workshop" />
      <el-table-column label="工序号" align="center" prop="operationNumber"/>

      <el-table-column label="故障开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="停机时间" align="center" prop="stopTime" />
      <el-table-column label="故障原因一级分类" align="center" prop="faultCauseOne">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gelly_fault_cause_one" :value="scope.row.faultCauseOne"/>
        </template>
      </el-table-column>
      <el-table-column label="故障原因二级分类" align="center" prop="faultCauseTwo">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gelly_fault_cause_two" :value="scope.row.faultCauseTwo"/>
        </template>
      </el-table-column>
      <el-table-column label="维修时间" align="center" prop="repairTime" />
      <el-table-column label="备件等待时间" align="center" prop="waitTime" />
      <el-table-column label="厂家服务时间" align="center" prop="serviceTime" />
      <el-table-column label="自主维修时间" align="center" prop="selfTime" />
      <el-table-column label="其他时间" align="center" prop="otherTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="故障名称" align="center" prop="faultName" />
      <el-table-column label="失效模式" align="center" prop="failureType" />
      <el-table-column label="问题描述" align="center" prop="problemDesc" />
      <el-table-column label="根本原因" align="center" prop="rootCause" />
      <el-table-column label="具体维修措施" align="center" prop="repairAction" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="维修人员" align="center" prop="technician" />

      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column label="备件名称" align="center" prop="partName" />
      <el-table-column label="备件型号" align="center" prop="partModel" />
      <el-table-column label="备件品牌" align="center" prop="brand" />
      <el-table-column label="消耗数量" align="center" prop="quantityUsed" />

      <el-table-column label="故障项目初始分类" align="center" prop="initCategory" />
      <el-table-column label="目前分类" align="center" prop="curCategory" />
      <el-table-column label="计划跟踪措施" align="center" prop="trackAction" />
      <el-table-column label="跟踪负责人" align="center" prop="responsible" />
      <el-table-column label="时间节点" align="center" prop="timeNode" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.timeNode, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="具体完成事项" align="center" prop="action" />
      <el-table-column label="是否复盘" align="center" prop="review" />
      <el-table-column label="责任人" align="center" prop="owner" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gelly:report:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gelly:report:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改故障报告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="维修单号" prop="repairNumber">
          <el-input v-model="form.repairNumber" placeholder="请输入维修单号" />
        </el-form-item>
        <el-form-item label="是否有保修单" prop="repairBill">
          <el-input v-model="form.repairBill" placeholder="请输入是否有保修单" />
        </el-form-item>
        <el-form-item label="故障项目初始分类" prop="initCategory">
          <el-input v-model="form.initCategory" placeholder="请输入故障项目初始分类" />
        </el-form-item>
        <el-form-item label="目前分类" prop="curCategory">
          <el-input v-model="form.curCategory" placeholder="请输入目前分类" />
        </el-form-item>
        <el-form-item label="计划跟踪措施" prop="trackAction">
          <el-input v-model="form.trackAction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="跟踪负责人" prop="responsible">
          <el-input v-model="form.responsible" placeholder="请输入跟踪负责人" />
        </el-form-item>
        <el-form-item label="时间节点" prop="timeNode">
          <el-date-picker clearable
            v-model="form.timeNode"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择时间节点">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="具体完成事项" prop="action">
          <el-input v-model="form.action" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否复盘" prop="review">
          <el-input v-model="form.review" placeholder="请输入是否复盘" />
        </el-form-item>
        <el-form-item label="责任人" prop="owner">
          <el-input v-model="form.owner" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="PDF" prop="filePath">
          <file-upload
            :file-size = "fileSize"
            :file-type = "fileType"
            :upload-file-url = "uploadFileUrl"
            v-model="form.filePath"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReport, getTrack, delTrack, addTrack, updateTrack } from "@/api/gelly/track";

export default {
  name: "Track",
  dicts: ['gelly_fault_cause_two', 'gelly_fault_cause_one','gelly_fault_category','gelly_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障报告表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairNumber:null,
        repairBill:null,
        deviceNumber:null,
        manufacturer:null,
        specifications:null,
        deviceName:null,
        workshop:null,
        operationNumber:null,
        faultName:null,
        failureType:null,
        problemDesc:null,
        rootCause:null,
        repairAction:null,
        technician:null,
        startTime:null,
        stopTime:null,
        faultCauseOne:null,
        faultCauseTwo:null,
        repairTime:null,
        waitTime:null,
        serviceTime:null,
        selfTime:null,
        otherTime:null,
        itemCode:null,
        partName:null,
        partModel:null,
        brand:null,
        quantityUsed:null,
        initCategory:null,
        curCategory:null,
        trackAction:null,
        responsible:null,
        timeNode:null,
        action:null,
        review:null,
        owner:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      uploadFileUrl : process.env.VUE_APP_BASE_API + "/gelly/report/uploadPdf",
      fileType: ["pdf"],
      fileSize: 10
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询故障报告列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      console.log(this.reportList)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        trackId: null,
        repairNumber: null,
        repairBill: null,
        initCategory: null,
        curCategory: null,
        trackAction: null,
        responsible: null,
        timeNode: null,
        action: null,
        review: null,
        owner: null,
        filePath: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.trackId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加故障报告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const trackId = row.trackId || this.ids
      getTrack(trackId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改故障报告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.trackId != null) {
            updateTrack(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrack(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const trackIds = row.trackId || this.ids;
      this.$modal.confirm('是否确认删除故障报告编号为"' + trackIds + '"的数据项？').then(function() {
        return delTrack(trackIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gelly/report/export', {
        ...this.queryParams
      }, `故障报告_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
<style>

</style>
